﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace WebApplication1
{
    public class DbHelp
    {
        // 连接字符串
        private static readonly string constr = "server = .;user id = sa;pwd = 123456;database = Admin";

        /// <summary>
        /// 封装查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable getSelect(string sql)
        {
            // 创建数据适配器对象
            SqlDataAdapter sqlData = new SqlDataAdapter(sql, constr);
            // 创建DataTable对象
            DataTable dt = new DataTable();
            // 数据填充
            sqlData.Fill(dt);
            // 返回数据表
            return dt;
        }

        /// <summary>
        /// 封装增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int getIDU(string sql)
        {
            // 创建数据库连接对象
            SqlConnection sqlcon = new SqlConnection(constr);
            // 创建SqlCommand对象
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            // 打开数据库
            sqlcon.Open();
            // 获取受影响的条数
            var result = sqlcom.ExecuteNonQuery();
            // 关闭数据库
            sqlcon.Close();
            // 返回受影响的条数
            return result;
        }
    }
}